package leecode

import (
	"container/list"
	"sort"
)

func findSecondMinimumValue(root *TreeNode) int {
	if root == nil {
		return 0
	}
	queue := list.New()
	hashMap := make(map[int]bool) //记录元素是否出现过
	queue.PushBack(root)
	var result []int
	for queue.Len() != 0 {
		size := queue.Len()
		temp := make([]int, 0)
		for i := 0; i < size; i++ {
			node := queue.Remove(queue.Front()).(*TreeNode)
			if node.Left != nil {
				queue.PushBack(node.Left)
			}
			if node.Right != nil {
				queue.PushBack(node.Right)
			}
			if !hashMap[node.Val] {
				temp = append(temp, node.Val)
			}

			hashMap[node.Val] = true

		}
		result = append(result, temp...)
	}
	sort.Ints(result)
	if len(result) < 2 {
		return -1
	}
	return result[1]
}
